<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Customer extends CrmController
{
    /**
     * 渲染模板customerList
     */
    public function customerList()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);

        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);

        $member = Cache::get('member');
        $this->assign("member",$member);

        // 模板输出
        return $this->fetch('customerList');
    }


    /**
     * 渲染模板editCustomer
     */
    public function editCustomer()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $customer_code = $_REQUEST['customer_code'];
        if (empty($customer_code)) {
            echo "页面出现未知错误！";
            exit;
        }
        $where = 'b.customer_code="'.$customer_code.'"';
        // 基本资料
        $customerData = Loader::model('Customer')->getCustomerData($where);
        // echo $where;exit;
        $customerData = $customerData['data'][0];
        // echo "<pre>";
        // print_r($customerData);exit;
        if ($customerData['sex'] == "男") {
            $customerData['option'] = '<option value="1" selected>男</option> <option value="2">女</option>';
        } elseif ($customerData['sex'] == "女") {
            $customerData['option'] = '<option value="1">男</option> <option value="2" selected>女</option>';
        } else {
            $customerData['option'] = '<option value="1">男</option> <option value="2">女</option>';
        }


        $users = Cache::get('users');
        $customerData['createuser'] = $users[$customerData['create_user']]['cn_name']."（".$customerData['create_user']."）";

        /*echo "<pre>";
        print_r($customerData);exit;*/
        $this->assign('customerData', $customerData);

        $address = Loader::model('Customer')->getAddress($customer_code);
        $this->assign('address', $address);
        // 模板输出
        return $this->fetch('editCustomer');
    }


    /**
     * 渲染模板importCustomer
     */
    public function importCustomer()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // 模板输出
        return $this->fetch('importCustomer');
    }


    /**
     * 渲染模板illness
     */
    public function illness()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $this->assign('customer_code', $_GET['customer_code']);
        // 模板输出
        return $this->fetch('illness');
    }


    /**
     * 渲染模板visit
     */
    public function visit()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $this->assign('customer_code', $_GET['customer_code']);
        // 模板输出
        return $this->fetch('visit');
    }


    /**
     * 渲染模板addIllness
     */
    public function addIllness()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $illnessSelect = Cache::get('illnessSelect');
        $illness_option = '';
        foreach ($illnessSelect as $val) {
            $illness_option .= '<option value="'.$val['id'].'">'.$val['illness_type'].'</option>';
        }
        $this->assign('illness_option', $illness_option);
        $this->assign('customer_code', $_GET['customer_code']);
        // 模板输出
        return $this->fetch('addIllness');
    }


    /**
     * 渲染模板addVisit
     */
    public function addVisit()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $this->assign('customer_code', $_GET['customer_code']);
        $result = Cache::get('visitSelect');
        // 回访类型
        $visit_type = '<option value="">请选择</option>';
        foreach ($result['visit_type'] as $val) {
            $visit_type .= '<option value="'.$val['id'].'">'.$val['visit_type'].'</option>';
        }
        $this->assign('visit_type', $visit_type);
        // 回访结果
        $visit_result = '<option value="">请选择</option>';
        foreach ($result['visit_result'] as $val) {
            $visit_result .= '<option value="'.$val['id'].'">'.$val['result_name'].'</option>';
        }
        $this->assign('visit_result', $visit_result);
        // 模板输出
        return $this->fetch('addVisit');
    }


    /**
     * 渲染模板editIllness
     */
    public function editIllness()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $id = $_REQUEST['id'];
        $result = Loader::model('Customer')->getIllnessInfo($id);
        $this->assign('illnessInfo', $result);
        $illnessSelect = Cache::get('illnessSelect');
        $illness_option = '';
        foreach ($illnessSelect as $val) {
            if ($result['illness_type'] == $val['id']) {
                $illness_option .= '<option value="'.$val['id'].'" selected>'.$val['illness_type'].'</option>';
                continue;
            }
            $illness_option .= '<option value="'.$val['id'].'">'.$val['illness_type'].'</option>';
        }
        $this->assign('illness_option', $illness_option);
        $this->assign('customer_code', $_GET['customer_code']);
        // echo $_GET['customer_code'];exit;
        // 模板输出
        return $this->fetch('editIllness');
    }


    /**
     * 渲染模板shareCustomer
     */
    public function taskCustomer()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 读缓存 获取回访类型信息
        $task = Cache::get('task');
        $options = "";
        foreach ($task as $key => $val) {
            $options .= "<option value='$key'>";
            $options .= $val['task_type'];
            $options .= "</option>";
        }
        $this->assign('options', $options);
        // 读缓存 获取销售方案信息
        $saleCache = Cache::get('saleProgram');
        $saleProgram = "";
        foreach ($saleCache as $key => $val) {
            $saleProgram .= "<option value='$key'>";
            $saleProgram .= $val['program_name'];
            $saleProgram .= "</option>";
        }
        $this->assign('saleProgram', $saleProgram);

        $customer_codes = rtrim($_GET['customer_codes'], '|');
        // echo $customer_codes;exit;
        $this->assign('customer_code', $customer_codes);
        // 模板输出
        return $this->fetch('taskCustomer');
    }


    /**
     * 新增回访任务
     */
    public function doTaskCustomer ()
    {
        $data = $_REQUEST;
        $data['createuser'] = $_SESSION['user_info']['username'];
        $data['createtime'] = date('Y-m-d H:i:s');
        $data['endtime'] = $_REQUEST['endtime'].' 23:59:59';
        // print_r($data);exit;

        Loader::model('Customer')->taskCustomer($data);
        // echo json_encode($ids);
    }


    /**
     * 渲染模板shareCustomer
     */
    public function transferCustomer()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // 读缓存 获取用户信息
        $users = Cache::get('users');
        $options = "";
        foreach ($users as $key => $val) {
            $options .= "<option value='$key'>";
            $options .= $val['cn_name'];
            $options .= "</option>";
        }
        $this->assign('options', $options);
        $customer_code = rtrim($_GET['customer_code'], '|');
        // echo $id;exit;
        $this->assign('customer_code', $customer_code);
        // 模板输出
        return $this->fetch('transferCustomer');
    }


    /**
     * 转交客户资料
     */
    public function dotransferCustomer ()
    {
        $customer_code = $_REQUEST['customer_code'];
        $create_user = empty($_REQUEST['username'])? '': $_REQUEST['username'];
        $transfer_note = $_REQUEST['transfer_note'];
        // print_r($create_user);exit;
        $customer_code = join('","',explode('|', $customer_code));
        $where = 'customer_code IN ("'.$customer_code.'")';
        $result = Loader::model('Customer')->transferCustomer($where, $create_user, $transfer_note);
        if ($result) {
            echo json_encode(array('success'=>true,'msg'=>'操作成功！'));
        } else {
            echo json_encode(array('msg'=>'操作失败！'));
        }
        // echo json_encode($ids);
    }


    /**
     * 资料导出
     */
    public function exportCustomer(){
        $where = "(1 ";
        $where .= empty($_REQUEST["recovery"])?"":" AND recovery = '".$_REQUEST["recovery"]."'";
        $where .= empty($_REQUEST["customer_type"])?" AND customer_type = 'first'":" AND customer_type = '".$_REQUEST["customer_type"]."'";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["import_mark"])?"":" AND import_mark like '%".$_REQUEST["import_mark"]."%'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND b.customer_code like '%".$_REQUEST["customer_code"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND create_user = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["integral"])?"":" AND integral ".$_REQUEST["integral"];
        $where .= empty($_REQUEST["member_level"])?"":" AND member_level = '".$_REQUEST["member_level"]."'";
        $where .= empty($_REQUEST["order_num"])?"":" AND order_num ".$_REQUEST["order_num"];
        $where .= empty($_REQUEST["sign_num"])?"":" AND sign_num ".$_REQUEST["sign_num"];
        $where .= empty($_REQUEST["age"])?"":" AND age = '".$_REQUEST["age"]."'";
        $where .= empty($_REQUEST["sex"])?"":" AND sex = '".$_REQUEST["sex"]."'";
        $where .= empty($_REQUEST["id_card"])?"":" AND id_card like '%".$_REQUEST["id_card"]."%'";
        
        // 曾经有一段逻辑清晰 思路严谨的代码放在 我面前
        // 我没有珍惜
        // 后来 有人让我改了 就有了下面这俩
        // 看似没问题的条件
        // 但我觉得有问题 但我又没试出来有啥问题
        // 就先当他们没问题吧
        if ($_REQUEST["last_call_time"]) {
            $last_call_time = explode(',', $_REQUEST["last_call_time"]);
            $where .= $this->lastTime($last_call_time[0], 'last_call_time');
            $where .= $this->lastTime($last_call_time[2], 'last_call_time');
        }

        if ($_REQUEST["last_order_time"]) {
            $last_order_time = explode(',', $_REQUEST["last_order_time"]);
            $where .= $this->lastTime($last_order_time[0], 'last_order_time');
            $where .= $this->lastTime($last_order_time[2], 'last_order_time');
        }
        $customer_codes = $_REQUEST['customer_codes'];
        if ($customer_codes) {
            // $customer_codes = '1|';
            $ids = explode('|', $customer_codes);
            $code_str = "(";
            foreach ($ids as $v) {
                $code_str .= '"'.$v.'",';
            }
            $code_str = rtrim($code_str, ',');
            $code_str .= ")";
            $where .= " AND b.customer_code IN $code_str";
        }
        $where .= " )";
        // echo $where;exit;
        Loader::model('Customer')->getExportCustomer($where);
    }


    /**
     * 导入excel
     */
    public function doImportCustomer ()
    {
        /*$file = request()->file('excel');
        var_dump($file);exit;*/
        Loader::model('Customer')->importExcel();
    }


    /**
     * 获取客户资料列表
     * @return [json] [客户资料列表]
     */
    public function customerDataList()
    {
        // $users = Cache::get('users');
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        //需先判断传值是否为空
        // print_r($_REQUEST);exit;
        $where = "(1 ";
        $where .= " AND recovery = 1";
        $where .= empty($_REQUEST["customer_type"])?" AND customer_type = 'first'":" AND customer_type = '".$_REQUEST["customer_type"]."'";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["import_mark"])?"":" AND import_mark like '%".$_REQUEST["import_mark"]."%'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND b.customer_code like '%".$_REQUEST["customer_code"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND create_user = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["integral"])?"":" AND integral ".$_REQUEST["integral"];
        $where .= empty($_REQUEST["member_level"])?"":" AND member_level = '".$_REQUEST["member_level"]."'";
        $where .= empty($_REQUEST["order_num"])?"":" AND order_num ".$_REQUEST["order_num"];
        $where .= empty($_REQUEST["sign_num"])?"":" AND sign_num ".$_REQUEST["sign_num"];
        $where .= empty($_REQUEST["age"])?"":" AND age = '".$_REQUEST["age"]."'";
        $where .= empty($_REQUEST["sex"])?"":" AND sex = '".$_REQUEST["sex"]."'";
        $where .= empty($_REQUEST["id_card"])?"":" AND id_card like '%".$_REQUEST["id_card"]."%'";
        

        if ($_REQUEST["last_call_time"]) {
            $last_call_time = explode(',', $_REQUEST["last_call_time"]);
            $where .= $this->lastTime($last_call_time[0], 'last_call_time');
            $where .= $this->lastTime($last_call_time[2], 'last_call_time');
        }

        if ($_REQUEST["last_order_time"]) {
            $last_order_time = explode(',', $_REQUEST["last_order_time"]);
            $where .= $this->lastTime($last_order_time[0], 'last_order_time');
            $where .= $this->lastTime($last_order_time[2], 'last_order_time');
        }


        /*$last_order_time = date('Y-m-d', time() - substr($_REQUEST["last_order_time"], 1)*24*3600);
        $where .= empty($_REQUEST["last_order_time"])?"":" AND last_order_time like '%".$last_order_time."%'";*/
        $where .= " )";
        // print_r($where);exit;

        $arrData = Loader::model('Customer')->getCustomerData($where,$page,$limit);
        echo json_encode($arrData);
    }


    /**
     * 只服务于 last_call_time 和 last_order_time ,其他地方如果用 出了bug概不负责
     * 他妈的处理一个条件硬是封装出来个函数 真是日了狗了
     * @param  [type] $time [description]
     * @param  [type] $key  [description]
     * @return [type]       [description]
     */
    protected function lastTime ($time, $key)
    {
        $where = ' ';
        if ($time) {
            $lastTime = date('Y-m-d', time() - substr($time, 1)*24*3600);
            if (substr($time, 0, 1) == '=') {
                $where .= " AND $key like '%".$lastTime."%'";
            } else if (substr($time, 0, 1) == '<') {
                $where .= " AND $key > '".$lastTime."'";
            } else {
                $where .= " AND $key < '".$lastTime."'";
            }
        }
        return $where;
    }


    /**
     * 删除客户资料
     * @return [type] [description]
     */
    public function delCustomer()
    {
        $customer_code = $_REQUEST["customer_code"];
        Loader::model('Customer')->delCustomer($customer_code);
    }


    /**
     * 更新客户资料
     */
    public function updateCustomer () 
    {
        $data = $_REQUEST;
        $location_address = explode('/', $data['location_address']);
        $address['province'] = $location_address[0];
        $address['city'] = $location_address[1];
        $address['area'] = $location_address[2];
        $address['address_details'] = $data['address_details'];
        $address['customer_code'] = $data['customer_code'];
        $address_return = Loader::model('Customer')->updateAddressCustomer($address);
        // print_r($address);exit;

        $basic['customer_code'] = $data['customer_code'];
        if(strpos($data['phone'],"*") === false) {
            $basic['phone'] = $data['phone'];
        }
        if(strpos($data['telephone'],"*") === false) {
            $basic['telephone'] = $data['telephone'];
        }
        $basic['name'] = $data['name'];
        $basic_return = Loader::model('Customer')->updateBasicCustomer($basic);
        // print_r($basic);

        $detail['customer_code'] = $data['customer_code'];
        $detail['sex'] = $data['sex'];
        $detail['age'] = $data['age'];
        $detail['member_level'] = $data['member_level'];
        $detail_return = Loader::model('Customer')->updateDetailCustomer($detail);
        // print_r($detail);
        /*print_r($address_return);
        print_r($basic_return);
        print_r($detail_return);exit;*/
        if ($address_return || $basic_return || $detail_return) {
            echo json_encode(array('success'=>true,'msg'=>'保存成功！'));
        } else {
            echo json_encode(array('msg'=>'保存失败！'));
        }
    }


    /**
     * 获取地区
     */
    public function getAddress ()
    {
        $customer_code = $_REQUEST['customer_code'];
        Loader::model('Customer')->getAddress($customer_code);
    }


    /**
     * 获取客户病情信息
     */
    public function getIllness ()
    {
        $customer_code = $_REQUEST['customer_code'];
        $where = "customer_code='$customer_code'";
        $res = Loader::model('Customer')->getIllness($where);
        echo json_encode($res);
    }


    /**
     * 获取客户病情信息
     */
    public function getVisit ()
    {
        $customer_code = $_REQUEST['customer_code'];
        $where = "customer_code='$customer_code'";
        $res = Loader::model('Customer')->getVisit($where);
        echo json_encode($res);
    }


    /**
     * 新增病情
     */
    public function insertIllness ()
    {
        $data = $_REQUEST;
        $data['create_user'] = $_SESSION['user_info']['username'];
        $data['create_time'] = date('Y-m-d H:i:s');
        // print_r($data);
        $result = Loader::model('Customer')->insertIllness($data);
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'保存成功！'));
        } else {
            echo json_encode(array('msg'=>'保存失败！'));
        }
    }


    /**
     * 新增回访记录
     */
    public function insertVisit ()
    {
        $data = $_REQUEST;
        $data['create_user'] = $_SESSION['user_info']['username'];
        $data['create_time'] = date('Y-m-d H:i:s');
        // print_r($data);exit;
        Loader::model('Customer')->insertVisit($data);
    }


    /**
     * 更新病情
     */
    public function updateIllness()
    {
        $data = $_REQUEST;
        Loader::model('Customer')->updateIllness($data);
        // print_r($_REQUEST);
    }


    /**
     * 删除病情
     */
    public function delIllness ()
    {
        $id = $_REQUEST['ids'];
        // $id = '1|';
        $ids = explode('|', $id);
        $id_str = "(";
        foreach ($ids as $v) {
            $id_str .= $v.',';
        }
        $id_str = rtrim($id_str, ',');
        $id_str .= ")";
        $where = "id IN $id_str";
        Loader::model('Customer')->delIllness($where);
    }


    /**
     * 删除回访记录
     */
    public function delVisit ()
    {
        $id = $_REQUEST['ids'];
        // $id = '1|';
        $ids = explode('|', $id);
        $id_str = "(";
        foreach ($ids as $v) {
            $id_str .= $v.',';
        }
        $id_str = rtrim($id_str, ',');
        $id_str .= ")";
        $where = "id IN $id_str";
        Loader::model('Customer')->delVisit($where);
    }


    /**
     * 渲染模板addVisit
     */
    public function editVisit()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $id = $_GET['id'];
        $visit_info = Loader::model('Customer')->getVisitInfo($id);
        //print_r($visit_info);exit;
        $this->assign('visit_info', $visit_info);

        $this->assign('customer_code', $_GET['customer_code']);
        $result = Cache::get('visitSelect');
        // 回访类型
        $visit_type = '<option value="">请选择</option>';
        foreach ($result['visit_type'] as $val) {
            if ($visit_info['visit_type_id'] == $val['id']) {
                $visit_type .= '<option value="'.$val['id'].'" selected>'.$val['visit_type'].'</option>';
            } else {
                $visit_type .= '<option value="'.$val['id'].'">'.$val['visit_type'].'</option>';
            }
        }
        $this->assign('visit_type', $visit_type);
        // 回访结果
        $visit_result = '<option value="">请选择</option>';
        foreach ($result['visit_result'] as $val) {
            if ($visit_info['visit_result_id'] == $val['id']) {
                $visit_result .= '<option value="'.$val['id'].'" selected>'.$val['result_name'].'</option>';
            } else {
                $visit_result .= '<option value="'.$val['id'].'">'.$val['result_name'].'</option>';
            }
        }
        $this->assign('visit_result', $visit_result);
        // 模板输出
        return $this->fetch('editVisit');
    }


    /**
     * 更新病情
     */
    public function updateVisit()
    {
        // print_r($_REQUEST);exit;
        $data = $_REQUEST;
        Loader::model('Customer')->updateVisit($data);
    }


    /**
     * 回收资料
     */
    public function recovery ()
    {
        $where = "(1 ";
        $where .= " AND recovery = 1";
        $where .= empty($_REQUEST["customer_type"])?" AND customer_type = 'first'":" AND customer_type = '".$_REQUEST["customer_type"]."'";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["import_mark"])?"":" AND import_mark like '%".$_REQUEST["import_mark"]."%'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND b.customer_code like '%".$_REQUEST["customer_code"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND create_user = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["integral"])?"":" AND integral ".$_REQUEST["integral"];
        $where .= empty($_REQUEST["member_level"])?"":" AND member_level = '".$_REQUEST["member_level"]."'";
        $where .= empty($_REQUEST["order_num"])?"":" AND order_num ".$_REQUEST["order_num"];
        $where .= empty($_REQUEST["sign_num"])?"":" AND sign_num ".$_REQUEST["sign_num"];
        $where .= empty($_REQUEST["age"])?"":" AND age = '".$_REQUEST["age"]."'";
        $where .= empty($_REQUEST["sex"])?"":" AND sex = '".$_REQUEST["sex"]."'";
        $where .= empty($_REQUEST["id_card"])?"":" AND id_card like '%".$_REQUEST["id_card"]."%'";
        
        if ($_REQUEST["last_call_time"]) {
            $last_call_time = explode(',', $_REQUEST["last_call_time"]);
            $where .= $this->lastTime($last_call_time[0], 'last_call_time');
            $where .= $this->lastTime($last_call_time[2], 'last_call_time');
        }

        if ($_REQUEST["last_order_time"]) {
            $last_order_time = explode(',', $_REQUEST["last_order_time"]);
            $where .= $this->lastTime($last_order_time[0], 'last_order_time');
            $where .= $this->lastTime($last_order_time[2], 'last_order_time');
        }
        $where .= " )";
        Loader::model('Customer')->recovery($where);
        // print_r($where);
    }


    /**
     * 新增数据任务 模板
     */
    public function dataTask ()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        
        return $this->fetch('dataTask');
    }


    /**
     * 新增数据任务
     */
    public function doDataTask ()
    {
        $data = $_REQUEST;
        $data['createtime'] = date('Y-m-d H:i:s');
        $data['createuser'] = $_SESSION['user_info']['username'];
        // print_r($data);exit;
        Loader::model('Customer')->dataTask($data);
    }


    /**
     * 新增数据任务 模板
     */
    public function addDataTask ()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // 客户编码
        $this->assign("customer_code",$_GET['customer_code']);
        $this->assign("customer_name",$_GET['customer_name']);
        // 归属人
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 任务编号
        $arrDataTask = Loader::model('Customer')->getDataTask();
        $this->assign("arrDataTask",$arrDataTask);
        return $this->fetch('addDataTask');
    }


    /**
     * 新增数据任务
     */
    public function doAddDataTask ()
    {
        $data = $_REQUEST;
        $data['addtime'] = date('Y-m-d H:i:s');
        // print_r($data);exit;
        Loader::model('Customer')->addDataTask($data);
    }


    /**
     * 选择数字条件
     */
    public function searchNum ()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        $this->assign("id",$_REQUEST['id']);
        return $this->fetch('searchNum');
    }



}